package com.joinway.mobile.service

class SqlFactory {

	protected String tablePrefix;
	
	public SqlFactory(String tablePrefix){
		this.tablePrefix = tablePrefix
	}
	
	public String createFindPruductSql(Integer catId = null, Integer goodsId = null){
		String sql = """
	select
		g.goods_id id
		, g.goods_name name
		, g.goods_number inventoryCount
		, shop_price price
		, gg.thumb_url photos
		, sum(og.goods_number) as selledCount
	from ${tablePrefix}_goods g 
		left join ${tablePrefix}_goods_gallery gg on g.goods_id = gg.goods_id
		left join ${tablePrefix}_order_goods og on g.goods_id = og.goods_id
		left join ${tablePrefix}_order_info oi on og.order_id = oi.order_id
	where oi.pay_status = 2
"""
		if(goodsId){
			sql += " and g.goods_id = ? "
		}
		
		if(catId){
			sql += " and cat_id = ? "
		}
		
		sql += " group by og.goods_id"
		
		sql
	}
	
	public static String createFindUpserProfileSql(int userId){
"""
	select
		case sex
			when 1 then '男'
			when 2 then '女'
			else '保密'
		end gender
		, u.mobile_phone cellPhone
		, e.content name
	from login_user u 
		left join reg_extend_info e on u.user_id = e.user_id
	where
		e.reg_field_id = 100
		and u.user_id = $userId
"""
	}
}
